package trangchu;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;

import chucnang.BanHang;
import chucnang.ButtonTabComponent;
import chucnang.ChungTu;
import chucnang.DoanhThu;
import chucnang.ChuyenKho;
import chucnang.MuaHang;
import chucnang.ThuTien;
import chucnang.TonKho;
import chucnang.TongHop;
import chucnang.TraTien;
import dangnhap.FromDangNhap;

public class ChucNang extends JPanel implements ActionListener {
	private JToolBar tbr;
	private JButton btnmuahang,btnbanhang,btnluanchuyen,btntonkho,btnthutien,
	btntratien,btnchungtu,btntonghop,btndoanhthu;
	private TrangChu trangchu;
	public ChucNang(TrangChu trangchu){
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.trangchu = trangchu;
		this.tbr = new JToolBar();
		this.add(this.tbr);
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnmuahang = new JButton("Nhập Hàng",new ImageIcon(FromDangNhap.rootPath+"/images/muahang1.gif"));
		this.btnmuahang.setVerticalTextPosition(JButton.BOTTOM);
		this.btnmuahang.setHorizontalTextPosition(JButton.CENTER);		
		this.tbr.add(this.btnmuahang);
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnbanhang = new JButton("Bán Hàng",new ImageIcon(FromDangNhap.rootPath+"images/banhang1.gif"));
		this.btnbanhang.setVerticalTextPosition(JButton.BOTTOM);
		this.btnbanhang.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnbanhang);
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnluanchuyen = new JButton("Chuyển Kho",new ImageIcon(FromDangNhap.rootPath+"images/luanchuyen.gif"));
		this.btnluanchuyen.setVerticalTextPosition(JButton.BOTTOM);
		this.btnluanchuyen.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnluanchuyen);
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btntonkho = new JButton("Tồn Kho",new ImageIcon(FromDangNhap.rootPath+"images/tonkho1.gif"));
		this.btntonkho.setVerticalTextPosition(JButton.BOTTOM);
		this.btntonkho.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btntonkho);
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnthutien = new JButton("Thu Tiền",new ImageIcon(FromDangNhap.rootPath+"images/thutien.gif"));
		this.btnthutien.setVerticalTextPosition(JButton.BOTTOM);
		this.btnthutien.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnthutien);
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btntratien = new JButton("Trả Tiền",new ImageIcon(FromDangNhap.rootPath+"images/tratien.gif"));
		this.btntratien.setVerticalTextPosition(JButton.BOTTOM);
		this.btntratien.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btntratien);
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btnchungtu = new JButton("Chứng Từ",new ImageIcon(FromDangNhap.rootPath+"images/chungtu.gif"));
		this.btnchungtu.setVerticalTextPosition(JButton.BOTTOM);
		this.btnchungtu.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btnchungtu);
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btntonghop = new JButton("Tổng Hợp",new ImageIcon(FromDangNhap.rootPath+"images/tonghop.gif"));
		this.btntonghop.setVerticalTextPosition(JButton.BOTTOM);
		this.btntonghop.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btntonghop);
		
		this.tbr.setOrientation(JToolBar.HORIZONTAL);
		this.btndoanhthu = new JButton("Doanh Thu",new ImageIcon(FromDangNhap.rootPath+"images/doanhthu.gif"));
		this.btndoanhthu.setVerticalTextPosition(JButton.BOTTOM);
		this.btndoanhthu.setHorizontalTextPosition(JButton.CENTER);
		this.tbr.add(this.btndoanhthu);
		
		// su kien
		this.btnmuahang.addActionListener(this);
		this.btnbanhang.addActionListener(this);
		this.btnluanchuyen.addActionListener(this);
		this.btntonkho.addActionListener(this);
		this.btnthutien.addActionListener(this);
		this.btntratien.addActionListener(this);
		this.btnchungtu.addActionListener(this);
		this.btntonghop.addActionListener(this);
		this.btndoanhthu.addActionListener(this);
		
	}
	public static Object currentTab;
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()== this.btnmuahang){
			
			int index = this.trangchu.tbnPaneCenter.positionTab("Nhập Hàng");
        	if(index > -1){	        		
        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new MuaHang(trangchu), "Nhập Hàng");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
	        }
	        			
		}
		if(e.getSource()== this.btnbanhang){

			int index = this.trangchu.tbnPaneCenter.positionTab("Bán Hàng");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new BanHang(trangchu), "Bán Hàng");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
			
		}
		if(e.getSource()== this.btnluanchuyen){
			int index = this.trangchu.tbnPaneCenter.positionTab("Chuyển Kho");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new ChuyenKho(trangchu), "Chuyển Kho");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		if(e.getSource()== this.btntonkho){
			int index = this.trangchu.tbnPaneCenter.positionTab("Tồn Kho");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new TonKho(trangchu), "Tồn Kho");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		if(e.getSource()== this.btnthutien){
			int index = this.trangchu.tbnPaneCenter.positionTab("Thu Tiền");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new ThuTien(trangchu), "Thu Tiền");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		if(e.getSource()== this.btntratien){
			int index = this.trangchu.tbnPaneCenter.positionTab("Trả Tiền");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new TraTien(trangchu), "Trả Tiền");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		if(e.getSource()== this.btnchungtu){
			int index = this.trangchu.tbnPaneCenter.positionTab("Chứng Từ");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new ChungTu(trangchu), "Chứng Từ");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		if(e.getSource()== this.btntonghop){
			int index = this.trangchu.tbnPaneCenter.positionTab("Tổng Hợp");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new TongHop(trangchu), "Tổng Hợp");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		if(e.getSource()== this.btndoanhthu){
			int index = this.trangchu.tbnPaneCenter.positionTab("Doanh Thu");
        	if(index > -1){	        		
        		this.trangchu.tbnPaneCenter.setSelectedIndex(index);
				return;
			}else if(index == -1){
				this.trangchu.tbnPaneCenter.addTab(new DoanhThu(trangchu), "Doanh Thu");
				this.trangchu.tbnPaneCenter.setSelectedIndex( this.trangchu.tbnPaneCenter.getTabCount()-1);
			}
		}
		int count = this.trangchu.tbnPaneCenter.getTabCount();
		for(int i=0;i<count;i++){
			this.trangchu.tbnPaneCenter.setTabComponentAt(i, new ButtonTabComponent(this.trangchu.tbnPaneCenter));
			
		}
		ChucNang.currentTab = this.trangchu.tbnPaneCenter.getComponentAt(this.trangchu.tbnPaneCenter.getSelectedIndex());
		
	}
}

